package com.mocha;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.mocha.bean.User;

@Controller
@RequestMapping("/springmvc")
public class MyController {

    
    private static final String SUCCESS = "success";
    
    
    @RequestMapping(value = "/index")
    public String test() {
        System.out.println("init index now .......");
        return "index";
    }
    
    @ModelAttribute
    public void getUser(@RequestParam(value = "id",required = false) Integer id,
            Map<String, Object> map) {
        if(!StringUtils.isEmpty(id)) {
            User user = new User(1, "Tom", "123456","tom@qq.com", 18);
            System.out.println("从数据库中获取user:"+user);
            
            map.put("user", user);
        }
    }
 
    @RequestMapping("/testModelAttribute")
    public String testModelAttribute(@ModelAttribute("user") User user) {
        System.out.println("修改："+user);
        return SUCCESS;
    }
    
    @RequestMapping("/helloView")
    public String testHelloView() {
        return "helloView";
    }
    
    
    
    
}
